package cn.stylefeng.guns.modular.rest;


import cn.stylefeng.guns.comm.bean.MainNewsResult;
import cn.stylefeng.guns.comm.util.DictTypeConstant;
import cn.stylefeng.guns.modular.comm.model.result.NewsResult;
import cn.stylefeng.guns.modular.comm.model.result.SwiperResult;
import cn.stylefeng.guns.modular.comm.service.NewsService;
import cn.stylefeng.guns.modular.comm.service.SwiperService;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 轮播图控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/miniprogramApi/main")
public class ApiMainController extends BaseController {
    @Autowired
    private SwiperService swiperService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private GunsProperties gunsProperties;
    @Autowired
    private DictService dictService;

    /**
     * 首页
     */
    @RequestMapping(value = "")
    public Object index() {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();
        // 轮播图
        List<SwiperResult> opens = swiperService.findOpens();
        opens.forEach(swiper -> {
            String substring = swiper.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1).replaceAll("\\\\", "/");
            swiper.setFilePath(substring);
        });
        map.put("opens", opens);

        // 新闻
//        List<NewsResult> top2News = newsService.findTop2News("00");
//        top2News.forEach(news -> {
//            if (news.getFilePath() != null) {
//                String substring = news.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1).replaceAll("\\\\", "/");
//                news.setFilePath(substring);
//            }
//            if (news.getTitle().length() > 13) {
//                news.setTitle(news.getTitle().substring(0, 13) + "...");
//            }
//        });
//        map.put("top2News", top2News);

        // 菜单
        List<Dict> dictList = dictService.listDictsByCode(DictTypeConstant.XWLX);
        map.put("menus", dictList);

        // 首页top
        List topItem = new ArrayList();
        map.put("topItems", topItem);
        dictList.stream().filter(dict -> ToolUtil.isNotEmpty(dict.getDescription()) && dict.getDescription().equals("top")).forEach(dict -> {
            List<NewsResult> top2News = newsService.findTop2News(dict.getCode());
            top2News.forEach(news -> {
                if (news.getFilePath() != null) {
                    String substring = news.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1).replaceAll("\\\\", "/");
                    news.setFilePath(substring);
                }
                if (news.getTitle().length() > 13) {
                    news.setTitle(news.getTitle().substring(0, 13) + "...");
                }
            });
            MainNewsResult mainNewsResult = new MainNewsResult();
            mainNewsResult.setTitle(dict.getName());
            mainNewsResult.setNews(top2News);
            topItem.add(mainNewsResult);
        });

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

}


